package library.router.interceptor;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;

/**
 * simple interceptor
 *
 * @author Created by qiang.hou on 2018/12/27.
 * @version 1.0
 */
public abstract class AbstractSimpleInterceptor implements IInterceptor {

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        if (postcard.getExtra() == interceptExtra() || postcard.getGroup().equals(interceptGroup())) {
            if (!needIntercept()) {
                callback.onContinue(postcard);
            } else {
                callback.onInterrupt(null);
                handleIntercept(postcard);
            }
        }
    }

    @Override
    public void init(Context context) {
    }

    /**
     * @return InterceptExtra
     */
    protected abstract int interceptExtra();

    /**
     * @return InterceptGroup
     */
    protected abstract String interceptGroup();

    /**
     * handle method when intercept
     */
    protected abstract void handleIntercept(Postcard postcard);

    /**
     * if need intercept
     *
     * @return boolean
     */
    protected abstract boolean needIntercept();
}
